import { searchCover, isString, isNumber } from "@/common.js"
// 处理用户的输入
export const handleInput = function (e) {
  let value = isString(e) || isNumber(e) ? e : e.target.value
  let errmsg = value.length > this.item.maxlength ? `文本最大长度为${this.item.maxlength}` : ''
  value = value.slice(0, this.item.maxlength)
  // 更新当前条目值
  const item = {...this.item, value, errmsg}
  this.$emit('setItem', item)
  // 更新整体表单的值
  let formData = searchCover(this.formData, {id: this.item.id}, v => ({...v, value, errmsg}))
  formData = formData.map(v => ({...v, children: v.type == 'split' ? searchCover(v.children, {id: this.item.id}, v => ({...v, value, errmsg})) : v.children}))
  this.$emit('setFormData', formData)
  eval(this.item.onChange)(e)
}
// 监听聚焦
export const handleFocus = function (e) {
  eval(this.item.onFocus)(e)
}
// 监听失去焦点
export const handleBlur = function (e) {
  eval(this.item.onBlur)(e)
}